﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Logic.Locations;

namespace GameApplication.MiniGame.SimpleGame
{
    /// <summary>
    ///     Логика взаимодействия для StonePaperScissors.xaml
    /// </summary>
    public partial class StonePaperScissors : Page, ISimpleGame
    {
        private static readonly List<ElementGame> List = new List<ElementGame>
        {
            ElementGame.Stone,
            ElementGame.Scissors,
            ElementGame.Paper
        };

        private static readonly Random R = new Random();
        private static readonly CustomRandom Random = new CustomRandom();


        private bool _endGame;
        private bool _win;
        private static int _rand;

        public StonePaperScissors()
        {
            InitializeComponent();


            ButtonPlayer1.Tag = ElementGame.Stone;
            ButtonPlayer2.Tag = ElementGame.Scissors;
            ButtonPlayer3.Tag = ElementGame.Paper;

            _rand = R.Next(3);
        }


        public event Action<bool> EndBattle;

        protected virtual void OnEndBattle(bool win)
        {
            Action<bool> handler = EndBattle;
            if (handler != null) handler(win);
        }

        private void UserStep(object sender, RoutedEventArgs e)
        {
            ButtonPlayer1.Visibility = Visibility.Collapsed;
            ButtonPlayer2.IsEnabled = false;
            ButtonPlayer3.Visibility = Visibility.Collapsed;

            Button button = sender as Button;
            if (button == null) return;
            ButtonPlayer2.Style = button.Style;

            ElementGame player = ((ElementGame) button.Tag);
            ElementGame enemy = List[GetElement()];
            ButtonEnemy.Style = GetStyle(enemy);

            if (player == enemy)
            {
                ResultBlock.Text = "Ничья";
            }
            else
            {
                _win = GetResult(player, enemy);
                ResultBlock.Text = _win ? "Победа" : "Поражение";
                _endGame = true;
            }

            ButtonNext.Visibility = Visibility.Visible;
            ResultBlock.Visibility = Visibility.Visible;
        }

        private static int GetElement()
        {
            int s;
            do
            {
                s = R.Next(3);
            } while (s == _rand);
            return _rand = s;
        }

        private Style GetStyle(ElementGame element)
        {
            Style result = (Style) FindResource("StyleNone");
            switch (element)
            {
                case ElementGame.Paper:
                    result = (Style) FindResource("StylePaper");
                    break;
                case ElementGame.Scissors:
                    result = (Style) FindResource("StyleScissors");
                    break;
                case ElementGame.Stone:
                    result = (Style) FindResource("StyleStone");
                    break;
            }
            return result;
        }

        private bool GetResult(ElementGame player, ElementGame enemy)
        {
            int p = (int) player;
            int e = (int) enemy;
            return p < e && Math.Abs(p - e) == 1 || p == 2 && e == 0;
        }

        private void ButtonNext_OnClick(object sender, RoutedEventArgs e)
        {
            if (_endGame)
            {
                OnEndBattle(_win);
            }
            else
            {
                ButtonPlayer1.Visibility = Visibility.Visible;
                ButtonPlayer2.IsEnabled = true;
                ButtonPlayer3.Visibility = Visibility.Visible;

                ButtonNext.Visibility = Visibility.Collapsed;
                ResultBlock.Visibility = Visibility.Collapsed;

                ButtonPlayer2.Style = (Style) FindResource("StyleScissors");
                ButtonEnemy.Style = (Style) FindResource("StyleNone");
            }
        }
    }

    internal enum ElementGame
    {
        /// <summary>
        ///     Камень
        /// </summary>
        Stone,

        /// <summary>
        ///     Ножницы
        /// </summary>
        Scissors,

        /// <summary>
        ///     Бумага
        /// </summary>
        Paper
    }
}